<?php
use think\Request;
use think\facade\Db;

/**
 * 打印
 */
function ff($data)
{
    print_r($data);die;
}

/** 
 * 密码加密 
 */
function setPass($pass)
{
    $authcode = 'leishan';
    $pass = "#>_<#" . md5(md5($authcode . $pass));
    return $pass;
}

/**
 * 获取当前域名
 */
function getUrl()
{
    return Request()->domain(1);
}

/**
 * 文件上传
 */
function fileUp($file, $path = 'image')
{
    $name = \think\facade\Filesystem::disk('public')->putFile($path, $file);
    $filePath = str_replace('\\','/',$name);
    return '/storage/'.$filePath;
}

/** 
 * 订单号 
 */
function getOrderNo()
{
    return date('Ymd'). str_pad(mt_rand(1, 99999), 5, 0, STR_PAD_LEFT);
}

/**
 * 发送验证码
 */
function sendSms($phone, $code)
{
    $CorpID = "CDJS008664";
    $Pwd = "zm0513@";
    $Mobile = $phone;
    $Content = "您的验证码为". $code ."，该验证码在5分钟之内有效，请勿泄露。【康业科技】";
    $SendTime = "";
    $Sms = 1;
    header("Content-type: text/html; charset=utf-8");
    date_default_timezone_set('PRC'); //设置默认时区为北京时间
    $ContentS = rawurlencode(mb_convert_encoding($Content, "gb2312", "utf-8"));
    $url = "https://sdk2.028lk.com/sdk2/BatchSend2.aspx?";
    $curpost = "CorpID=".$CorpID."&Pwd=".$Pwd."&Mobile=".$Mobile."&Content=".$ContentS."&SendTime=".$SendTime;

    if($Sms == 1){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查 -https
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_URL, $url.$curpost);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $result = curl_exec($ch);
        curl_close($ch);
        //$result = file_get_contents($url.$curpost);
    }else if($Sms == 2){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查 -https
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curpost);
        $result = curl_exec($ch);
        curl_close($ch);
    }else{
        $result = 0;
    }

    return $result;
}

/** 
 * 查询系统参数 
 */
function getSystemVal()
{
    return Db::name('system')->where('id', 1)->find();
}

/**
 * 模拟get请求
*/
function curl_get($url)
{
    $headers = array('User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36');
    $oCurl = curl_init();

    if (stripos($url, "https://") !== FALSE) {
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
    }

    curl_setopt($oCurl, CURLOPT_TIMEOUT, 20);
    curl_setopt($oCurl, CURLOPT_URL, $url);
    curl_setopt($oCurl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
    $sContent = curl_exec($oCurl);
    $aStatus = curl_getinfo($oCurl);
    curl_close($oCurl);

    if (intval($aStatus["http_code"]) == 200) {
        return $sContent;
    } else {
        return false;
    }
}